#pragma semicolon 1
#pragma newdecls required // 强制新语法
#include <sourcemod>
#include <sdktools>
#include <adminmenu>

TopMenu hTopMenu;
TopMenuObject adminkillToTopMenu = INVALID_TOPMENUOBJECT; //武器









#define PLUGIN_VERSION "1.0"
#define PLUGIN_FLAG FCVAR_SPONLY|FCVAR_NOTIFY

ConVar C_FriendFire;
int G_FriendFire;
public Plugin myinfo =
{
	name = "l4d2_admin_kill_fool",
	author = "凌鹿",
	description = "管理员秒杀玩家插件",
	version = PLUGIN_VERSION,
	url = ""
}

public void OnPluginStart()
{
	RegAdminCmd("sm_yskill", Command_PlayerHurt, ADMFLAG_KICK, "管理员开关友伤秒杀");
	C_FriendFire = CreateConVar("l4d2_seckill_friend_fire", "0", "友伤秒杀 [1=开|0=关].", PLUGIN_FLAG, true, 0.00, true, 1.00);
	C_FriendFire.AddChangeHook(convar_changed);
	HookEvent("player_hurt", Event_PlayerHurt);
	SetConVarFloat(FindConVar("z_health"),  1.0);
	//AutoExecConfig(true, "l4d2_Seckill");
}


//----------------------------------------------------------------admin面板-----------
public void OnAdminMenuReady(Handle aTopMenu)
{
	TopMenu topmenu = TopMenu.FromHandle(aTopMenu);

	if (topmenu == hTopMenu)
		return;
	
	hTopMenu = topmenu;
	
	TopMenuObject objDifficultyMenu = FindTopMenuCategory(hTopMenu, "OtherFeatures");  //OtherFeatures 改成别的名字和下面同一个名字
	if (objDifficultyMenu == INVALID_TOPMENUOBJECT)
		objDifficultyMenu = AddToTopMenu(hTopMenu, "OtherFeatures", TopMenuObject_Category, AdminMenuHandler, INVALID_TOPMENUOBJECT);  //OtherFeatures 改成别的名字和上面同一个名字
	
	adminkillToTopMenu= AddToTopMenu(hTopMenu,"sm_yskill",TopMenuObject_Item,adminkillMenuHandler,objDifficultyMenu,"sm_yskill",ADMFLAG_ROOT);  //秒杀代码
}

public void AdminMenuHandler(Handle topmenu, TopMenuAction action, TopMenuObject object_id, int param, char[] buffer, int maxlength)
{
	if (action == TopMenuAction_DisplayTitle)
	{
		Format(buffer, maxlength, "选择功能:", param);
	}
	else if (action == TopMenuAction_DisplayOption)
	{
		Format(buffer, maxlength, "其它功能", param); //改成你需要的菜单名字
	}
}

public void adminkillMenuHandler(Handle topmenu, TopMenuAction action, TopMenuObject object_id, int param, char[] buffer, int maxlength)
{
	if (action == TopMenuAction_DisplayOption)
	{
		if (object_id == adminkillToTopMenu)
			Format(buffer, maxlength, "秒杀玩家", param);   //改插件名字
	}
	else if (action == TopMenuAction_SelectOption)
	{
		if (object_id == adminkillToTopMenu)
			Command_PlayerHurt(param, 0);  //这里改
	}
}

//----------------------------------
//------------------------------------














void get_cvars()
{
	G_FriendFire = C_FriendFire.IntValue;
}

void convar_changed(ConVar convar, const char[] oldValue, const char[] newValue)
{
	get_cvars();
}

public void OnConfigsExecuted()
{
	get_cvars();
}

public Action Command_PlayerHurt(int client, int args)
{
	if (G_FriendFire)
	{
		C_FriendFire.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x05友伤秒杀已\x03关闭\x05.");
	}
	else
	{
		C_FriendFire.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x05友伤秒杀已\x03开启\x05.");
	}
	return Plugin_Handled;
}

public void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	int userid = GetClientOfUserId(GetEventInt(event, "userid"));
	int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	if(G_FriendFire == 1)
	{
		if(IsClientInGame(userid) && IsPlayerAlive(userid))
		{
			//友伤互秒
			if (GetClientTeam(userid) == 2 && GetClientTeam(attacker) == 2)
			{
				if (!bCheckClientAccess(userid))
				    {
					    ForcePlayerSuicide(userid);
						
					}
				if (!bCheckClientAccess(userid))
                    {	
                      	PrintToChatAll("\x05[提示]\x03%N\x04杀死了\x03%N.", attacker, userid);	
                    }						
			}
			
		}
	}
}



bool bCheckClientAccess(int client)
{
	if(GetUserFlagBits(client) & ADMFLAG_ROOT)
		return true;
	return false;
}